﻿

namespace Mulala.Cavan.Presentation.Mvc.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Spring.Context;
    using Spring.Context.Support;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Presentation.Seedwork;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Core.Helper;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Presentation.Mvc.Controller;

    public static class NannyViewModelConverter
    {
        public static void Convert(Nanny nanny, INannyViewModel nannyViewModel)
        {
            IApplicationContext springContext = ContextRegistry.GetContext();
            IAttachmentAppService attachmentAppService = springContext.GetObject(MulalaAppServiceConst.ATTACHMENTAPPSERVICE) as IAttachmentAppService;
            IAreaAppService areaAppService = springContext.GetObject(MulalaAppServiceConst.AREAAPPSERVICE) as IAreaAppService;

            nannyViewModel.Area = areaAppService.GetById(nanny.Area.ID).Name;
            nannyViewModel.Age = nanny.Age.ConvertToTagText();
            if (nanny.Sex == NannyIdentifier.Sex.Female)
            {
                nannyViewModel.Sex = "女";
            }
            else if (nanny.Sex == NannyIdentifier.Sex.Male)
            {
                nannyViewModel.Sex = "男";
            }
            nannyViewModel.Nation = nanny.Nation.ConvertToTagText();
            nannyViewModel.Astrology = nanny.Astrology.ConvertToTagText();
            nannyViewModel.ComputerLevel = nanny.ComputerLevel.ConvertToTagText();
            nannyViewModel.ChineseZodiac = nanny.ChineseZodiac.ConvertToTagText();
            nannyViewModel.IsMarried = nanny.IsMarried ? "已婚" : "未婚";
            nannyViewModel.Degree = nanny.Degree.ConvertToTagText();
            nannyViewModel.CanDrive = nanny.CanDrive ? "有" : "无";
            nannyViewModel.HasChildren = nanny.HasChildren ? "有" : "无";
            nannyViewModel.Healthy = nanny.Healthy.ConvertToTagText();
            nannyViewModel.Faith = nanny.Faith.ConvertToTagText();
            nannyViewModel.IsTrained = nanny.IsTrained ? "有" : "无";
            nannyViewModel.Experience = nanny.Experience.ConvertToTagText();
            nannyViewModel.MainSkill = nanny.MainSkill.ToPositionArray().ConvertToTagText(DictionaryIdentifier.GroupType.NannySkill);
            nannyViewModel.Avatar = attachmentAppService.GetNannyAvatar(nanny);
            nannyViewModel.UserAccountIcon = GetNannyUserAccountIcon(nanny.UserAccount.Score);
            nannyViewModel.ExperienceIcon = GetNannyExperienceIcon(nanny.UserAccount.Experience);
            nannyViewModel.DetailUrl = Project.Settings.HostServer + "/" + ControllerConst.NANNY + "/" + ActionConst.DETAIL + "/" + nanny.ID.ToString();
            nannyViewModel.Item = nanny;
        }

        public static void ConvertForEdit(INannyViewModel nannyViewModel, Nanny nanny)
        {
            nannyViewModel.Certificates = nanny.Certificate.ToPosition();
            nannyViewModel.Languages = nanny.Language.ToPosition();
            nannyViewModel.MainSkills = nanny.MainSkill.ToPosition();
            nannyViewModel.SexForEdit = nanny.Sex.GetHashCode();
            nannyViewModel.TypeForEdit = nanny.Type.GetHashCode();
            nannyViewModel.CityID = nanny.Area.ID.Substring(0, 4);
            nannyViewModel.TownID = nanny.Area.ID;
            foreach (string s in nannyViewModel.Certificates)
            {
                nannyViewModel.Certificate = nannyViewModel.Certificate + s + ",";
            }
            foreach (string s in nannyViewModel.Languages)
            {
                nannyViewModel.Language = nannyViewModel.Language + s + ",";
            }
            foreach (string s in nannyViewModel.MainSkills)
            {
                nannyViewModel.MainSkill = nannyViewModel.MainSkill + s + ",";
            }
        }

        public static void ConvertForEdit(Nanny nanny, INannyViewModel nannyViewModel)
        {
            nanny.MainSkill = StringHelper.ClearLastChar(nannyViewModel.MainSkill).ToBinaryValue();
            nanny.Certificate = StringHelper.ClearLastChar(nannyViewModel.Certificate).ToBinaryValue();
            nanny.Language = StringHelper.ClearLastChar(nannyViewModel.Language).ToBinaryValue();
            nanny.Sex = (NannyIdentifier.Sex)nannyViewModel.SexForEdit;
            nanny.Type = (NannyIdentifier.NannyType)nannyViewModel.TypeForEdit;
        }

        public static IList<INannyViewModel> Convert(IList<Nanny> nannies, bool isConvertAll)
        {
            IApplicationContext springContext = ContextRegistry.GetContext();
            IAttachmentAppService attachmentAppService = springContext.GetObject(MulalaAppServiceConst.ATTACHMENTAPPSERVICE) as IAttachmentAppService;
            IList<INannyViewModel> nannyViewModels = new List<INannyViewModel>();
            if (null != nannies)
            {
                foreach (Nanny nanny in nannies)
                {
                    INannyViewModel nannyViewModel = ModelFactory.CreateModel<INannyViewModel>();
                    if (isConvertAll)
                    {
                        NannyViewModelConverter.Convert(nanny, nannyViewModel);
                    }
                    else
                    {
                        nannyViewModel.Type = nanny.Type.GetHashCode().ToString().ConvertToTagText(DictionaryIdentifier.GroupType.NannyType);
                    }
                    nannyViewModel.Avatar = attachmentAppService.GetNannyAvatar(nanny);
                    nannyViewModel.Item = nanny;

                    nannyViewModels.Add(nannyViewModel);
                }
            }

            return nannyViewModels;
        }

        private static string GetNannyUserAccountIcon(int score)
        {
            if (score <= Project.Settings.NormalUserAccountMaxScore)
            {
                return string.Empty;
            }
            else if (score < Project.Settings.BronzeUserAccountMaxScore)
            {
                return Project.Settings.BronzeUserAccountIcon;
            }
            else if (score < Project.Settings.SilverUserAccountMaxScore)
            {
                return Project.Settings.SilverUserAccountIcon;
            }
            else if (score < Project.Settings.GoldUserAccountMaxScore)
            {
                return Project.Settings.GoldUserAccountIcon;
            }
            else if (score < Project.Settings.DiamondUserAccountMaxScore)
            {
                return Project.Settings.DiamondUserAccountIcon;
            }
            return string.Empty;
        }

        private static string GetNannyExperienceIcon(int experience)
        {
            if (experience <= Project.Settings.NannyOneLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "1";
            }
            else if (experience < Project.Settings.NannyTwoLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "2";
            }
            else if (experience < Project.Settings.NannyTreeLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "3";
            }
            else if (experience < Project.Settings.NannyFourLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "4";
            }
            else if (experience < Project.Settings.NannyFiveLevelMaxExperience)
            {
                return Project.Settings.ExperienceIcon + "5";
            }
            return string.Empty;
        }
    }
}
